package com.huan.dml;

import com.huan.Call;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class GetDataAPI {


    //获取数据(get)
    public static void getData(String tableName,String rowkey,String cf,String cn) throws IOException {
        //1.获取表对象
        Connection conn = Call.conn;
        Table table = conn.getTable( TableName.valueOf( tableName ) );

        //2.创建get对象
        Get get = new Get( Bytes.toBytes( rowkey ) );

//        //2.1指定获取的列族
//        get.addFamily( Bytes.toBytes( cf ) );
//
//        //2.2指定列族和列
//        get.addColumn( Bytes.toBytes( cf ),Bytes.toBytes( cn ) );
//
//        //2.3指定版本
//        get.setMaxVersions(5);

        //3.获取数据
        Result results = table.get( get );

        //4.解析result并打印
        Cell[] cells = results.rawCells();

        for (Cell cell : cells) {
            //遍历数据信息
            System.out.println("CF:"+Bytes.toString( CellUtil.cloneFamily( cell ) )
            +",CN:"+Bytes.toString( CellUtil.cloneQualifier( cell ) ) +
                    ",Value:"+ Bytes.toString( CellUtil.cloneValue( cell ) )
            );

        }
    }



    public static void main(String[] args) throws IOException {
        getData("student","1001","info","name");

        Call.close();

    }
}
